package com.chainyoung.component_tmperson.ui.fragment;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chainyoung.common.adpter.BaseFragmentAdapter;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.base.BaseFragment;
import com.chainyoung.common.common_widget.CustomViewPager;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.event.BaseEvent;
import com.chainyoung.common.view.VerticalSwipeRefreshLayout;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.ui.fragment.WalletCloudListFragment;
import com.chainyoung.component_tmperson.ui.fragment.WalletCloudListFragment1;
import com.chainyoung.component_tmperson.ui.fragment.WalletLocalListFragment;
import com.chainyoung.component_tmperson.ui.fragment.WalletLocalListFragment1;
import com.chainyoung.router.RouterConstants;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.simple.eventbus.Subscriber;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


@Route(path = RouterConstants.PATH_MARKET_WALLET)
public class WalletFragment1 extends BaseFragment {


    @BindView(R2.id.iv_back)
    ImageView ivBack;
    @BindView(R2.id.rb_cloud)
    RadioButton rbCloud;
    @BindView(R2.id.rb_local)
    RadioButton rbLocal;
    @BindView(R2.id.rg)
    RadioGroup rg;
    @BindView(R2.id.iv_toolbar_right)
    ImageView ivToolbarRight;
    @BindView(R2.id.view_pager)
    ViewPager viewPager;
    @BindView(R2.id.refresh_layout)
    VerticalSwipeRefreshLayout refreshLayout;
    @BindView(R2.id.appbar_layout)
    AppBarLayout appbar_layout;
    @BindView(R2.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R2.id.coordinator_layout)
    CoordinatorLayout coordinatorLayout;


    private List<Fragment> fragmentList = new ArrayList<>();
    private boolean isVisible = true;


    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {

    }

    @Override
    protected void initUi() {
        super.initUi();

    }

    private void initViewPager() {
        //refreshLayout.setEnableLoadMore(false);

        fragmentList.add(WalletCloudListFragment1.newInstance(isVisible));
        fragmentList.add(WalletLocalListFragment1.newInstance(isVisible));
        viewPager.setAdapter(new BaseFragmentAdapter(getChildFragmentManager(), fragmentList));
        viewPager.setCurrentItem(0);
        rlTitle.setBackgroundColor(ContextCompat.getColor(mContext, R.color._4461f5));
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_cloud) {
                    viewPager.setCurrentItem(0, false);
                } else {
                    viewPager.setCurrentItem(1, false);
                }
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    rbCloud.setChecked(true);
                    coordinatorLayout.setBackgroundResource(0);
                    rlTitle.setBackgroundColor(ContextCompat.getColor(mContext, R.color._4461f5));
                } else {
                    rbLocal.setChecked(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        refreshLayout.setColorSchemeColors(ContextCompat.getColor(mContext, R.color._061c69));
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                switch (viewPager.getCurrentItem()) {
                    case 0:
                        ((WalletCloudListFragment1) fragmentList.get(0)).loadData();
                        break;
                    case 1:
                        ((WalletLocalListFragment1) fragmentList.get(1)).getWallets();
                        break;
                }
            }
        });

    }

    public void finishRefresh() {
        if (refreshLayout != null) {
            refreshLayout.setRefreshing(false);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onFirstUserVisible() {
        initViewPager();

    }

    @Override
    public void onUserVisible() {
        super.onUserVisible();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_fragment_wallet1;
    }


    @Subscriber
    void onGetNewWallet(BaseEvent event) {
        if (event.getCode() == BaseEvent.EventCode.IS_GET_NEW_WALLET) {
            viewPager.setCurrentItem(1, false);
            //setVisibleView();
        }
    }


    @Subscriber
    void onRefreshBg(BaseEvent event) {
        if (event.getCode() == BaseEvent.EventCode.REFRESH_WALLET_BG) {
            boolean isShowNoHdWallet = (boolean) event.getData();
            if (isShowNoHdWallet) {
                coordinatorLayout.setBackgroundResource(R.mipmap.icon_wallet_bg);
              //  rlTitle.setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
            } else {
                coordinatorLayout.setBackgroundResource(0);
                rlTitle.setBackgroundColor(ContextCompat.getColor(mContext, R.color._4461f5));
            }
        }
    }


//
//    @Subscriber
//    void setAssetVisible(BaseEvent event) {
//        if (event.getCode() == BaseEvent.EventCode.REFRESH_TOTAL_ASSET) {
//            String localAsset = ((WalletLocalListFragment) fragmentList.get(1)).getTotalAsset();
//            String cloudAsset = ((WalletCloudListFragment) fragmentList.get(0)).getTotalAsset();
//            totalAsset = new BigDecimal(localAsset).add(new BigDecimal(cloudAsset)).setScale(2, RoundingMode.DOWN) + "";
//            setVisibleView();
//        }
//    }









}
